#include <vector>
#include <queue>
#include <iostream>

using namespace std;

class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<int> ans;
        ans.reserve(numCourses);
        vector<int> input_num(numCourses, 0);
        vector<vector<int>> route_path(numCourses);
        for (vector<int> num : prerequisites) {
            input_num[num[0]]++;
            route_path[num[1]].push_back(num[0]);
        }
        queue<int> zero_input;
        for (int i = 0; i < input_num.size(); i++) {
            if (input_num[i] == 0) {
                zero_input.push(i);
            }
        }
        while (zero_input.size() > 0) {
            int index = zero_input.front();
            zero_input.pop();
            ans.push_back(index);
            input_num[index] = -1;
            for (int i = 0; i < route_path[index].size(); i++) {
                if (input_num[route_path[index][i]] != -1) {
                    input_num[route_path[index][i]]--;
                }
                if (input_num[route_path[index][i]] == 0) {
                    zero_input.push(route_path[index][i]);
                }
            }
        }
        for (int num : input_num) {
            if (num != -1) {
                return {};
            }
        }
        return ans;
    }
};

int main() {
    vector<vector<int>> input = {{1, 0}, {2, 0}, {3, 1}, {3, 2}};
    Solution solution;
    vector<int> output = solution.findOrder(4, input);
    for (int val : output) {
        cout << val << endl;
    }
    return 0;
}